﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arbore;

namespace PresentationWpf.DB
{
    public class PopulateDB : DBWorker
    {
        public void Populate(ref Arbore.Arbore arb,string Nume)
        {
            using (Database1Entities instance = EntityConnectionManager.Instance.GetNewEntityConnection)
            {
                Nod m = new Nod() { Childrens = arb.Radacina.Childrens, ParentNode = arb.Radacina.ParentNode, Value = arb.Radacina.Value };
                Nodul nd= new Nodul(){ID=Guid.NewGuid(),Value=m.Value,NodParinte=null};
                instance.AddToNoduls(nd);
                Arbor Arb=  new Arbor(){Id=Guid.NewGuid(),Nume=Nume,Radacina=nd.ID};
                instance.AddToArbori(Arb);
                 instance.SaveChanges();
                 nodLoder(ref m, nd);
            }
        }
        private void nodLoder( ref Nod nod,Nodul n)
        {
            using (Database1Entities instance = EntityConnectionManager.Instance.GetNewEntityConnection)
            {
                int i = 0;
                foreach (Nod nd in nod.Childrens)
                {
                    Nod g = nd;
                    Nodul ng = new Nodul() { ID = Guid.NewGuid(), Value = nd.Value, NodParinte = n.ID };
                    instance.AddToNoduls(ng);
                    instance.SaveChanges();
                    nodLoder(ref g, ng);
                }
            }

        }
    }
}
